<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/5
 * Time: 17:42
 */

namespace app\index\lib;

class Wxchat
{
    protected $appid;
    protected  $AppSecret;
    public function __construct()
    {
        $this->appId = 'wx75bbccf096140543';
        $this->AppSecret = 'e0d2be1c99791809c08b3127013959f6';
    }
    /**
     *获取code
     *@param  $scope
     */
    public  function getcod($scope)
    {
        $url =  "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
        $urli = urlencode($url);
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf2eb73321c89f05a&redirect_uri=".$urli."&response_type=code&scope=".$scope."&state=STATE#wechat_redirect";

        header('Location:'.$url);
    }
    /**
     *获取openID和access_token
     * @param code
     * @return array
     */
    public function getUserOpenId()
    {
//            if (!isset($_GET['code']) || !empty($_GET['code']))
//            {
//               redirect('/index/Index/index');
//            }

        $code = $_GET['code'];
        $data['access_token'] = session('access_token');
        $data['openid'] =session('openId');
        if ($data['access_token'] == false || $data['openid'] == false)
        {
            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf2eb73321c89f05a&secret=4145d22783b266a307240e6712f39047&code=' . $code . '&grant_type=authorization_code';
            $re = $this->http_curl($url);
            if (strstr($re, 'access_token'))
            {
                $data = json_decode($re,true);
                session('access_token', $data['access_token'],7000);
                session('openid', $data['openid'],7000);
            }
        }

        return $data;
    }
    /**
     *获取用户信息信息
     * @param $openId
     * @param  $access_token
     * @return  json
     */
    public function getUserInfo($openId,$access_token)
    {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openId."&lang=zh_CN";
        return $this->http_curl($url);
    }

    public function getAccessToken()
    {
        $access_token = cache('baseAccess_token');
        if (!$access_token){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->AppSecret";
            $re = json_decode($this->http_curl($url),true) ;
            if (isset($re['access_token']) ){
                cache('baseAccess_token',$re['access_token'],7000);
                $access_token = cache('baseAccess_token');
            }
        }
        return $access_token;
    }

    public function jsapi_ticket($accesstoken)
    {
        $jsapi_ticket = cache('jsapi_ticket');
        if (!$jsapi_ticket){
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accesstoken";
            $re = json_decode($this->http_curl($url),true) ;
            if (isset($re['ticket']) ){
                cache('jsapi_ticket',$re['ticket'],7000);
                $jsapi_ticket = cache('jsapi_ticket');
            }
        }
        return $jsapi_ticket;
    }

    public function http_curl($url)
    {
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;
    }




}